/*
 * Copyright (c) 2016 zhangcm. All Rights Reserved.
 */
package com.jnzc.ocr.rest.app.shop.controller.app;

import com.jnzc.ocr.common.bean.Page;
import com.jnzc.ocr.common.bean.PageResult;
import com.jnzc.ocr.common.vo.UserVo;
import com.jnzc.ocr.dependencies.rest.base.BaseController;
import com.jnzc.ocr.rest.app.shop.param.app.AppSupplierSaveParam;
import com.jnzc.ocr.serv.shop.api.ShopSupplierApi;
import com.jnzc.ocr.serv.shop.entity.ShopSupplier;
import com.jnzc.ocr.serv.shop.filter.ShopSupplierFilter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author zhangcm
 * @version 1.0, 2017/9/13
 */
@Api(tags = {"app - 供应商接口"})
@RestController
@RequestMapping("/app/suppliers")
public class AppSupplierController extends BaseController {

    @Autowired
    private ShopSupplierApi shopSupplierApi;

    @ApiOperation(value = "分页")
    @GetMapping("")
    public PageResult<ShopSupplier> search(Page page){
        UserVo userVo = super.get();
        ShopSupplierFilter supplierFilter = new ShopSupplierFilter();
        supplierFilter.setShopId(userVo.getShopId());
        return shopSupplierApi.search(supplierFilter, page.getPageNo(), page.getPageSize());
    }

    @ApiOperation(value = "详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "供应商ID", required = true, paramType = "path")
    })
    @GetMapping("/{id}")
    public ShopSupplier info(@PathVariable Long id){
        UserVo userVo = super.get();
        return shopSupplierApi.find(id, userVo.getShopId());
    }

    @ApiOperation(value = "保存")
    @PostMapping("")
    public void save(@RequestBody AppSupplierSaveParam param){
        UserVo userVo = super.get();
        ShopSupplier obj;
        if(null == param.getId()){
            obj = new ShopSupplier();
            obj.setShopId(userVo.getShopId());
        } else {
            obj = shopSupplierApi.find(param.getId(), userVo.getShopId());
        }
        obj.setName(param.getName());
        obj.setAccessType(param.getAccessType());
        obj.setAccessTypeName(null);
        obj.setSupplierCategoryId(param.getSupplierCategoryId());
        obj.setSupplierCategoryName(null);
        obj.setAddress(param.getAddress());
        obj.setContactWay(param.getContactWay());
        obj.setStockAmount(param.getStockAmount());
        obj.setArrearAmount(param.getArrearAmount());
        shopSupplierApi.save(obj);
    }

    @ApiOperation(value = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "供应商ID", required = true, paramType = "path")
    })
    @DeleteMapping("/{id}")
    public void delete(@PathVariable Long id){
        UserVo userVo = super.get();
        shopSupplierApi.delete(id, userVo.getShopId());
    }
}
